import time

import requests
import json
from threading import Thread, Lock
from queue import Queue
from lxml import etree


class XiaomiSpider:
    def __init__(self):
        self.url = 'https://www.maoyan.com/films?showType=1&offset={}'
        # 创建队列
        self.q = Queue()
        self.lock = Lock()

    def url_in(self):
        # 将url如队列
        for page in range(2):
            url = self.url.format(page * 30)
            self.q.put(url)

    def parse_html(self):
        while True:
            # 上锁
            self.lock.acquire()
            if not self.q.empty():
                url = self.q.get()
                # 释放锁
                self.lock.release()
                headers = {
                    'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50'
                }
                html = requests.get(url=url, headers=headers).text
                print("网页数据"+html)
                p = etree.HTML(html)
                list_1 = p.xpath('//dd/div[@class="channel-detail movie-item-title"]/@title')
                print(list_1)
            else:
                self.lock.release()
                break

    def run(self):
        self.url_in()
        t_list = []
        for i in range(2):
            t = Thread(target=self.parse_html)
            t_list.append(t)
            t.start()
        for t in t_list:
            t.join()


if __name__ == '__main__':
    start_time = time.time()
    spider = XiaomiSpider()
    spider.run()
    end_time = time.time()
    print(start_time, "----", end_time)
